// pages/search/search.js
const config = require('../../config.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.basePath,
        fullLoading: true,
        articleLoding: true,
        articleData: [],
        total: 0,
        pageIndex: 1,
        title: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            title: options.title
        });
        wx.setNavigationBarTitle({
            title: options.title + '-搜索结果'
        });
        this.getArticleList();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.setData({
            fullLoading: false
        });
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        if (that.data.total <= that.data.articleData.length) {
            return;
        }
        that.setData({
            pageIndex: that.data.pageIndex + 1
        });
        that.getArticleList();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onShareTimeline(res) {
        return {
            title: this.data.title + "-搜索结果",
            path: '/pages/search/search?title=' + this.data.title
        }
    },
    getArticleList: function () {
        let that = this;
        that.setData({
            articleLoging: true
        });
        wx.request({
            url: config.basePath + '/mini/article/list',
            method: 'GET',
            data: {
                pageIndex: that.data.pageIndex,
                pageSize: 6,
                title: that.data.title
            },
            success(res) {
                if (res.data.code === 200) {
            
                    that.setData({
                        articleData: that.data.articleData.concat(res.data.data),
                        total: res.data.total,
                        articleLoding: true
                    });

                    if (that.data.total <= that.data.articleData.length) {
                        that.setData({
                            articleLoding: false
                        });
                    }
                } else {
                    wx.lin.showMessage({
                        type: 'error',
                        content: '服务出错'
                    });
                }
            },
            fail(err) {
                that.setData({
                    articleLoding: false
                });
                wx.lin.showMessage({
                    type: 'error',
                    content: '网络出错'
                })
            }
        });
    }
})